package com.socket.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {
	public static void main(String[] args) throws Exception {
		ServerSocket welcomeSocket = new ServerSocket(8080);
		while (true) {
			Socket connectSocket = welcomeSocket.accept();
			new Thread(new S(connectSocket)).start();
		}
	}
}

class S implements Runnable {
	Socket s;

	public S(Socket s) {
		this.s = s;
	}

	public void run() {

		ServerActionController controller = new ServerActionController(s);
		boolean flag = true;
		boolean isEnd;

		// 头部信息
		while (true) {
			try {
				if (!flag) {
					System.out.println("有错误发生");
					isEnd = controller.receiveActionAfterError();
					flag = true;
				} else {
					isEnd = controller.receiveAction();
				}

			} catch (SocketException e) {
				System.out.println(e.log);

				flag = false;
				continue;
			}
			if (isEnd) {
				controller.close();
				try {
					s.close();
					System.out.println("结束");
					break;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
	}
}